From: Brad Jorsch Date: Wed, 31 Aug 2016 19:18:26 +0000 (-0400) Subject: ApiParamInfo: Allow fetching info for all submodules of a module X-Git-Tag: 1.31.0-rc.0~5810^2 X-Git-Url: http://git.cyclocoop.org/%7D%7Cconcat%7B?a=commitdiff_plain;h=be5c2ca0174cbbcce17a43dcf634b2b7ae9433c5;p=lhc%2Fweb%2Fwiklou.git ApiParamInfo: Allow fetching info for all submodules of a module This allows fetching info for all query submodules with modules=query+*, and info for all modules recursively with modules=main|main+**. Bug: T28680 Change-Id: Icca5d2fda39d9d43fe20d77e34779412cec77aad --- diff --git a/RELEASE-NOTES-1.28 b/RELEASE-NOTES-1.28 index 6639a95be8..600ac7012f 100644 --- a/RELEASE-NOTES-1.28 +++ b/RELEASE-NOTES-1.28 @@ -96,6 +96,8 @@ production. ApiPageSet may contain entries where the 'from' value is percent-encoded as the raw value cannot be represented in a valid API response. These are indicated by a 'fromencoded' boolean alongside the existing 'from' parameter. +* (T28680) action=paraminfo can now return info about all submodules of a + module without listing them all explicitly. === Action API internal changes in 1.28 === * Added a new hook, 'ApiMakeParserOptions', to allow extensions to better diff --git a/includes/api/ApiParamInfo.php b/includes/api/ApiParamInfo.php index 25e1a7f8e1..caf0cd76fc 100644 --- a/includes/api/ApiParamInfo.php +++ b/includes/api/ApiParamInfo.php @@ -46,7 +46,39 @@ class ApiParamInfo extends ApiBase { $this->context->setLanguage( $this->getMain()->getLanguage() ); if ( is_array( $params['modules'] ) ) { - $modules = $params['modules']; + $modules = []; + foreach ( $params['modules'] as $path ) { + if ( $path === '*' || $path === '**' ) { + $path = "main+$path"; + } + if ( substr( $path, -2 ) === '+*' || substr( $path, -2 ) === ' *' ) { + $submodules = true; + $path = substr( $path, 0, -2 ); + $recursive = false; + } elseif ( substr( $path, -3 ) === '+**' || substr( $path, -3 ) === ' **' ) { + $submodules = true; + $path = substr( $path, 0, -3 ); + $recursive = true; + } else { + $submodules = false; + } + + if ( $submodules ) { + try { + $module = $this->getModuleFromPath( $path ); + } catch ( UsageException $ex ) { + $this->setWarning( $ex->getMessage() ); + } + $submodules = $this->listAllSubmodules( $module, $recursive ); + if ( $submodules ) { + $modules = array_merge( $modules, $submodules ); + } else { + $this->setWarning( "Module $path has no submodules" ); + } + } else { + $modules[] = $path; + } + } } else { $modules = []; } @@ -69,6 +101,8 @@ class ApiParamInfo extends ApiBase { $formatModules = []; } + $modules = array_unique( $modules ); + $res = []; foreach ( $modules as $m ) { @@ -121,6 +155,29 @@ class ApiParamInfo extends ApiBase { $result->addValue( null, $this->getModuleName(), $res ); } + /** + * List all submodules of a module + * @param ApiBase $module + * @param boolean $recursive + * @return string[] + */ + private function listAllSubmodules( ApiBase $module, $recursive ) { + $manager = $module->getModuleManager(); + if ( $manager ) { + $paths = []; + $names = $manager->getNames(); + sort( $names ); + foreach ( $names as $name ) { + $submodule = $manager->getModule( $name ); + $paths[] = $submodule->getModulePath(); + if ( $recursive && $submodule->getModuleManager() ) { + $paths = array_merge( $paths, $this->listAllSubmodules( $submodule, $recursive ) ); + } + } + } + return $paths; + } + /** * @param array $res Result array * @param string $key Result key @@ -449,8 +506,10 @@ class ApiParamInfo extends ApiBase { protected function getExamplesMessages() { return [ - 'action=paraminfo&modules=parse|phpfm|query+allpages|query+siteinfo' + 'action=paraminfo&modules=parse|phpfm|query%2Ballpages|query%2Bsiteinfo' => 'apihelp-paraminfo-example-1', + 'action=paraminfo&modules=query%2B*' + => 'apihelp-paraminfo-example-2', ]; } diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index a68a87f764..974e0aa128 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -296,13 +296,14 @@ "apihelp-options-example-complex": "Reset all preferences, then set skin and nickname.", "apihelp-paraminfo-description": "Obtain information about API modules.", - "apihelp-paraminfo-param-modules": "List of module names (values of the action and format parameters, or main). Can specify submodules with a +.", + "apihelp-paraminfo-param-modules": "List of module names (values of the action and format parameters, or main). Can specify submodules with a +, or all submodules with +*, or all submodules recursively with +**.", "apihelp-paraminfo-param-helpformat": "Format of help strings.", "apihelp-paraminfo-param-querymodules": "List of query module names (value of prop, meta or list parameter). Use $1modules=query+foo instead of $1querymodules=foo.", "apihelp-paraminfo-param-mainmodule": "Get information about the main (top-level) module as well. Use $1modules=main instead.", "apihelp-paraminfo-param-pagesetmodule": "Get information about the pageset module (providing titles= and friends) as well.", "apihelp-paraminfo-param-formatmodules": "List of format module names (value of format parameter). Use $1modules instead.", "apihelp-paraminfo-example-1": "Show info for [[Special:ApiHelp/parse|action=parse]], [[Special:ApiHelp/jsonfm|format=jsonfm]], [[Special:ApiHelp/query+allpages|action=query&list=allpages]], and [[Special:ApiHelp/query+siteinfo|action=query&meta=siteinfo]].", + "apihelp-paraminfo-example-2": "Show info for all submodules of [[Special:ApiHelp/query|action=query]].", "apihelp-parse-description": "Parses content and returns parser output.\n\nSee the various prop-modules of [[Special:ApiHelp/query|action=query]] to get information from the current version of a page.\n\nThere are several ways to specify the text to parse:\n# Specify a page or revision, using $1page, $1pageid, or $1oldid.\n# Specify content explicitly, using $1text, $1title, and $1contentmodel.\n# Specify only a summary to parse. $1prop should be given an empty value.", "apihelp-parse-param-title": "Title of page the text belongs to. If omitted, $1contentmodel must be specified, and [[API]] will be used as the title.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 650acb978e..abbc69b03d 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -285,6 +285,7 @@ "apihelp-paraminfo-param-pagesetmodule": "{{doc-apihelp-param|paraminfo|pagesetmodule}}", "apihelp-paraminfo-param-formatmodules": "{{doc-apihelp-param|paraminfo|formatmodules}}", "apihelp-paraminfo-example-1": "{{doc-apihelp-example|paraminfo}}", + "apihelp-paraminfo-example-2": "{{doc-apihelp-example|paraminfo}}", "apihelp-parse-description": "{{doc-apihelp-description|parse}}", "apihelp-parse-param-title": "{{doc-apihelp-param|parse|title}}", "apihelp-parse-param-text": "{{doc-apihelp-param|parse|text}}",